import cinemaView from '../views/cinema.art'
import cinemaModel from '../models/cinema'

class Cinema{
    constructor(){
        this.list = [];
        this.offset = 0;
    }
    renderer(list){
        
        let cinemaListHtml = cinemaView({
            list
        })

        $('main').html(cinemaListHtml)
        $('.city').html(localStorage.getItem('cityHistory') || '北京');
        $('.city').on('tap',function(){  //城市路由点击
            location.hash = 'city'
        })
        $('.input').on('tap',function(){
            location.hash = 'search'
        })
        $('.item').on('tap',function(){
            location.hash = $(this).attr('data-to')
        })
    }

    async render(){
        let that =this
        let result = await cinemaModel.get({
            offset : this.offset,
            cityId : localStorage.getItem('cityId') ||('1')
        })
        let list = this.list = result.cinemas;
        this.renderer(
            list
        )
        $('main').scroll(async function(){
            if($('main').height()+$('main').scrollTop()==$('.cinema-container').height()){
                that.offset += 20
                let result = await cinemaModel.get({
                    offset :that.offset,
                    cityId :localStorage.getItem('cityId')||('1')
                })
                let list = this.list = result.cinemas
                that.list = [...that.list,...list]
                that.renderer(
                    that.list
                )
            }
        })

    }
    
}

export default new Cinema()